/* eslint-disable quotes */
const fs = require('fs')
const path = require('path')

interface Option {
  file?: string;
  code?: string
}

const resolve = (file: string) => path.resolve(__dirname, file)

export function executeJavaScript (browser: any, option: Option) {
  let code = option.code
  try {
    if (option.file) code = fs.readFileSync(resolve(option.file), 'utf-8')
    browser.webContents.executeJavaScript(code).then((res: any) => {
      res && console.log(res) // Will be the JSON object from the fetch call
    })
  } catch (error) {
    error && console.warn(`${option.file} is not found`)
  }
}
